package tiposFueraDeLaAPI;

import iterables.FactoriaDeAgregados;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class BolsaImpl<T> implements Bolsa<T>{

	private List<T> l;
	
	public BolsaImpl (){
		l = FactoriaDeAgregados.<T>creaList(); 
	}
	
	public int count(Object o) {
		return Collections.frequency(l, o);
	}

	public boolean add(T arg0) {
		return l.add(arg0);
	}

	public boolean addAll(Collection<? extends T> arg0) {
		return l.addAll(arg0);
	}

	public void clear() {
		l.clear();
	}

	public boolean contains(Object arg0) {
		return l.contains(arg0);
	}

	public boolean containsAll(Collection<?> arg0) {
		return l.containsAll(arg0);
	}

	public boolean isEmpty() {
		return l.isEmpty();
	}

	public Iterator<T> iterator() {
		Set<T> s = FactoriaDeAgregados.creaSet();
		s.addAll(l);
		
		return s.iterator();
	}

	public boolean remove(Object arg0) {
		return l.remove(arg0);
	}

	public boolean removeAll(Collection<?> arg0) {
		return l.removeAll(arg0);
	}

	public boolean retainAll(Collection<?> arg0) {
		return l.retainAll(arg0);
	}

	public int size() {
		return l.size();
	}

	public Object[] toArray() {
		return l.toArray();		
	}

	public <T2>T2[] toArray(T2[] arg0) {
		return l.toArray(arg0);
	}
	@SuppressWarnings("unchecked")//para que no aparezcan los warnings
	public boolean equals(Object o){
		boolean r=o instanceof Bolsa && size()==((Bolsa)o).size();		
		
		if(r)
			for(T e:this)
				r=r&&count(e)==((Bolsa)o).count(e);
				
		return r;
	}
	
	public String toString(){
		String res = "[";
		for(T o:l)
			res += o.toString() + ",";
		res=res.substring(0, res.length()-1); // para quitar la coma al final
		res+="]";
		return res;
	}

}